package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
//import com.jt.tenant.conn.SetDataRecharge;
//import com.jt.tenant.conn.SetEquipResult;
//import com.jt.tenant.conn.SetInsPayResult;
//import com.jt.tenant.conn.SetOrderResult;
//import com.jt.tenant.conn.SetPayResult;
//import com.jt.tenant.dialog.ResultDialog;
import com.jt.tenant.widget.Tools;
import com.module.zfb.PayAction;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by 马长鸣 on 2016/5/14.
 */
public class GSPaymentActivity extends JTActivity implements View.OnClickListener {

    private TextView title_name;

    private LinearLayout zfb_payment, wx_payment, wo_payment;

    private TextView go_pay;

    private ImageView iv_zfb, iv_wx, iv_wopay;

    private String order_id, total_price, gs_name, gs_address, lon, lat, flag, ZFB_ACCOUNT, WX_ACCOUNT, commonCount;

    private String type;

    private int payType = 100;

//    private SetPayResult setPayResult;
//
//    private SetOrderResult setOrderResult;
//
//    private SetEquipResult setEquipResult;
//
//    private SetInsPayResult setInsPayResult;
//
//    private SetDataRecharge setDataRecharge;

    public static wxSuccess wxSuccess;

    private String goodsname, expand;


    @Override
    protected void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gs_payment);

        order_id = getIntent().getStringExtra("order_id");

        total_price = getIntent().getStringExtra("total_price");

        gs_name = getIntent().getStringExtra("gs_name");

        gs_address = getIntent().getStringExtra("gs_address");

        ZFB_ACCOUNT = getIntent().getStringExtra("ZFB_ACCOUNT");

        WX_ACCOUNT = getIntent().getStringExtra("WX_ACCOUNT");

        lon = getIntent().getStringExtra("lon");

        lat = getIntent().getStringExtra("lat");

        flag = getIntent().getStringExtra("flag");

        commonCount = getIntent().getStringExtra("commonCount");

        goodsname = getIntent().getStringExtra("goodsname");

        expand = getIntent().getStringExtra("expand");


//        setPayResult = new SetPayResult(new AsyCallBack<SetPayResult.Info>() {
//            @Override
//
//            public void onSuccess(String toast, int type, SetPayResult.Info info) throws Exception {
//
//                super.onSuccess(toast, type, info);
//
//                if (info.msg.equals("000")) {
//
//                    startActivity(new Intent(GSPaymentActivity.this, OilQrcodeActivity.class)
//
//                            .putExtra("order_id", order_id)
//
//                            .putExtra("gs_name", gs_name)
//
//                            .putExtra("gs_address", gs_address)
//
//                            .putExtra("lon", lon)
//
//                            .putExtra("lat", lat));
//
//
//                } else {
//
//                }
//            }
//
//
//
//            @Override
//
//            public void onFail(String toast, int type) throws Exception {
//
//                super.onFail(toast, type);
//
//            }
//        });
//
//        setPayResult.code = order_id;
//
//        setOrderResult = new SetOrderResult(new AsyCallBack<SetOrderResult.Info>() {
//
//            @Override
//            public void onSuccess(String toast, int type, SetOrderResult.Info info) throws Exception {
//
//                if (info.msg.equals("000")) {
//
//                    startActivity(new Intent(GSPaymentActivity.this, MyOrderActivity.class).putExtra("id", 1));
//
//                }
//            }
//
//            @Override
//            public void onFail(String toast, int type) throws Exception {
//
//                super.onFail(toast, type);
//            }
//        });
//
//        setEquipResult = new SetEquipResult(new AsyCallBack<SetEquipResult.Info>(){
//
//            @Override
//            public void onSuccess(String toast, int type, SetEquipResult.Info info) throws Exception {
//
//                super.onSuccess(toast, type, info);
//
//                if (info.msg.equals("000")){
//
//                    UtilToast.show(context,"购买成功，我们将立即发货");
//
//                    JTApplication.INSTANCE.finishActivity(GSPaymentActivity.class,SaleEquipActivity.class,PayEquipActivity.class);
//
//                }else {
//
//                    UtilToast.show(context,info.msg);
//
//                    JTApplication.INSTANCE.finishActivity(GSPaymentActivity.class,PayEquipActivity.class);
//
//                }
//            }
//
//            @Override
//            public void onFail(String toast, int type) throws Exception {
//
//                super.onFail(toast, type);
//
//                UtilToast.show(context,toast);
//
//            }
//        });
//
//        setInsPayResult = new SetInsPayResult(new AsyCallBack<SetInsPayResult.Info>(){
//
//            @Override
//            public void onSuccess(String toast, int type, SetInsPayResult.Info info) throws Exception {
//
//                super.onSuccess(toast, type, info);
//
//                if(info.msg.equals("000")){
//
//                    ResultDialog dialog = new ResultDialog(context);
//
//                    JTApplication.jtPreferences.saveInsFlag("6");
//
//                    dialog.setTip("支付成功，我们将立即为您生成保单，请耐心等待。");
//
//                    dialog.setActivity("com.jt.tenant.activity.MainActivity");
//
//                    dialog.show();
//
//                }
//
//            }
//
//        });

//        setDataRecharge = new SetDataRecharge(new AsyCallBack<SetDataRecharge.Info>(){
//
//            @Override
//            public void onSuccess(String toast, int type, SetDataRecharge.Info info) throws Exception {
//                super.onSuccess(toast, type, info);
//
//                if (info.msg.equals("000")){
//
//                    UtilToast.show(context,"充值成功，流量套餐将于下月生效");
//
//                    JTApplication.jtPreferences.saveUserIntegral(Tools.F_num(String.valueOf(Double.parseDouble(JTApplication.jtPreferences.readUserIntegral("2"))-Integer.parseInt(commonCount))),"2");
//
//                    JTApplication.INSTANCE.finishActivity(GSPaymentActivity.class);
//
//
//                }else {
//
//                    UtilToast.show(context,info.msg);
//
//                }
//
//
//            }
//
//        });

        wxSuccess = new wxSuccess() {
            @Override
            public void success(String payType) {

                type = payType;

                payResult();

            }
        };


        initView();
    }

    private void initView() {


        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("选择支付方式");

        zfb_payment = (LinearLayout) findViewById(R.id.zfb_payment);

        wx_payment = (LinearLayout) findViewById(R.id.wx_payment);

        wo_payment = (LinearLayout) findViewById(R.id.wo_payment);

        iv_zfb = (ImageView) findViewById(R.id.iv_pay_zfb);

        iv_wx = (ImageView) findViewById(R.id.iv_pay_wx);

        iv_wopay = (ImageView) findViewById(R.id.iv_pay_wopay);

        go_pay = (TextView) findViewById(R.id.go_pay);

        wo_payment.setOnClickListener(this);

//        wo_payment.setVisibility(View.GONE);

        zfb_payment.setOnClickListener(this);

        wx_payment.setOnClickListener(this);

        go_pay.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.zfb_payment:

                choosePay(0);

                break;

            case R.id.wx_payment:

                choosePay(1);

                break;

            case R.id.wo_payment:

                choosePay(2);

                break;

            case R.id.go_pay:

                if (payType == 0) {

                    type = "0";

                    try {

                        new PayAction(this) {

                            @Override
                            protected void onSuccess() {

                                payResult();

                            }

                            @Override
                            protected void onEnd() {

                            }

                        }.pay("支付", JTApplication.jtPreferences.readUserID(), order_id, total_price);
//                    .pay("支付", JTApplication.jtPreferences.readUserID(), order_id, total_price,ZFB_ACCOUNT);

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                } else if (payType == 1) {

                    JTApplication.lbjzwxPay.pay("支付", order_id, Tools.F_num(String.valueOf(Double.parseDouble(total_price) * 100)));

//                JTApplication.lbjzwxPay.pay("支付", order_id, "1");


                } else if (payType == 2) {

                    startActivity(new Intent(this, WoPayActivity.class)

                            .putExtra("GoodsName", "简途科技")

                            .putExtra("Expand", "简途科技")

                            .putExtra("PayBalance", String.valueOf(Double.parseDouble(total_price)))

                    );

                }else {

                    UtilToast.show(context,"请选择支付方式");

                }

                break;

        }
    }

    private void payResult() {

        switch (flag) {

            case "1":

//                setPayResult.payType = type;
//
//                setPayResult.execute(GSPaymentActivity.this);

                break;

            case "2":

//                setOrderResult.code = order_id;
//
//                setOrderResult.payType = type;
//
//                setOrderResult.execute(GSPaymentActivity.this);

                break;

            case "3":

//                setEquipResult.code = order_id;
//
//                setEquipResult.payType = type;
//
//                setEquipResult.payState = "1";
//
//                setEquipResult.payMoney = total_price;
//
//                setEquipResult.execute(GSPaymentActivity.this);

                break;

            case "4":

//                setInsPayResult.payType = type;
//
//                setInsPayResult.price = total_price;
//
//                setInsPayResult.execute(GSPaymentActivity.this);

                break;

            case "5":

//                setDataRecharge.orderId = order_id;
//
//                setDataRecharge.commonCount = commonCount;
//
//                setDataRecharge.price = total_price;
//
//                setDataRecharge.type = type;
//
//                setDataRecharge.payType = type;
//
//                setDataRecharge.execute(GSPaymentActivity.this);

                break;

            default:

                break;
        }


    }

    public abstract class wxSuccess {

        public abstract void success(String payType);

    }

    private void choosePay(int flag) {

        payType = flag;

        switch (flag) {

            case 0:

                iv_zfb.setImageResource(R.mipmap.icon_pay_on);

                iv_wx.setImageResource(R.mipmap.icon_pay_off);

                iv_wopay.setImageResource(R.mipmap.icon_pay_off);

                break;

            case 1:

                iv_zfb.setImageResource(R.mipmap.icon_pay_off);

                iv_wx.setImageResource(R.mipmap.icon_pay_on);

                iv_wopay.setImageResource(R.mipmap.icon_pay_off);

                break;

            case 2:

                iv_zfb.setImageResource(R.mipmap.icon_pay_off);

                iv_wx.setImageResource(R.mipmap.icon_pay_off);

                iv_wopay.setImageResource(R.mipmap.icon_pay_on);

                break;

        }

    }

    @Override
    protected void onResume() {

        super.onResume();

        payType = 100;


    }
}
